local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHOLYBUIBase());
local CommonOHOH =_G.CommonOHOH
local ConnectClass=CommonOHOH.load.Connect();
local Debris=game:GetService("Debris")
function InitInsts(ui)
	ui.insts = {};
    ui.insts.eggViewport ="ctrl.egg.Viewport"
    ui.insts.eggCF ="ctrl.egg.CF"
    ui.insts.eggAnimationController ="ctrl.egg.AnimationController"
    ui.insts.eggPart ="ctrl.egg.EggPart"
    ui.insts.eggAnimation ="ctrl.egg.animation"
    ui.insts.petRoot ="ctrl.pet.root"
    ui.insts.petCell ="ctrl.pet.cell"
end


function M.new(...)
	return M(...);
end;
function M.ctor(self)
	InitInsts(self)

	M.super.ctor(self, ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.HatchEgg2PetGui"));
	self.HatchDoneEvent=ConnectClass.new()

    self.insts.petCell.Visible=false
   self.animTrack= self.insts.eggAnimationController:LoadAnimation(self.insts.eggAnimation)

end


function M.dtor(self)
	M.super.dtor(self);
end;

function M.init(self,ctrl)
    self.Ctrl=ctrl
end

local CellClass=require(script.cell)

function M.ShowPets(self,petList)

    self.List={}
    for k,petId in pairs(petList) do
        local guid=petId.."_"..k
        local obj=self.insts.petCell:Clone()
        obj.Visible=true
        obj.Parent=self.insts.petRoot
        local cell=CellClass.new(self,petId,guid,obj)
        self.List[guid]=cell
        cell:Play(1)
        task.wait()
    end
    ClientOHOH.load.Client_AudioManager():PlayGloble("petsAppear")
    self:AddProcess(function()
        self:Wait(3)
        for _,cell in pairs(self.List) do
            cell:dtor()
        end
        self.HatchDoneEvent:Action()
    end)

end

function M.HatchShow(self,eggId,petList)

    -- print("------HatchShow----------.--",eggId)
   local egg=self.Ctrl:CreateEggMesh(eggId)
   egg.CFrame=self.insts.eggCF
   egg.Parent=self.insts.eggViewport
   local w=Instance.new("WeldConstraint",egg)
   w.Part0=egg
   w.Part1=self.insts.eggPart
   self.animTrack:Play()
   self:AddProcess(function()
        self:Wait(self.animTrack.Length) --用于播放蛋的动画
        egg:Destroy()
        self:ShowPets(petList)
   end)
   ClientOHOH.load.Client_AudioManager():PlayGloble("hatchEgg")
end;




return M
